package client;

import java.io.*;
import java.net.*;

public class Client {
    BufferedReader reader;
    PrintWriter writer;
    Socket sock ;
    public PrintWriter getWriter(){
    	return writer;
    }
	public void setUpNetworking(String p,int n){
    	try{
   		    sock=new Socket(p,n);
    		InputStreamReader streamReader=new InputStreamReader(sock.getInputStream());
    		reader=new BufferedReader(streamReader);
    		writer=new PrintWriter(sock.getOutputStream());
    	}catch(IOException ex){
    		ex.printStackTrace();
    	}
    }
	
	public Boolean is(String id,String pass){
		Boolean result=false;
		try{
    		writer.println(id);
    		writer.flush();
    		writer.println(pass);
			writer.flush();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		try {
			if(reader.readLine().equals("yes")){
				result=true;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	public void readData() throws IOException{	
		File file1=new File("Book.txt");
		FileWriter f1=new FileWriter(file1);
		BufferedWriter bw1=new BufferedWriter(f1);
		String message=reader.readLine();
		String input="";
		while((message!=null)&&(!(message.equals("//")))){			
			input=input+message+"\n";
			message=reader.readLine();
		}
		bw1.write(input);
		bw1.close();
		File file2=new File("Sales.txt");
		FileWriter f2=new FileWriter(file2);
		BufferedWriter bw2=new BufferedWriter(f2);
		input="";
		message=reader.readLine();
		while((message)!=null&&(!(message.equals("//")))){
			input=input+message+"\n";
			message=reader.readLine();
		}
		bw2.write(input);
		bw2.close();
		File file3=new File("DengLu.txt");
		FileWriter f3=new FileWriter(file3);
		BufferedWriter bw3=new BufferedWriter(f3);
		message=reader.readLine();
		input="";
		while((message)!=null&&(!(message.equals("//")))){
			input=input+message+"\n";
			message=reader.readLine();
		}
		bw3.write(input);
		bw3.close();
		File file4=new File("Prices.txt");
		FileWriter f4=new FileWriter(file4);
		BufferedWriter bw4=new BufferedWriter(f4);
		message=reader.readLine();
		input="";
		while((message)!=null&&(!(message.equals("//")))){
			input=input+message+"\n";
			message=reader.readLine();
		}
		bw4.write(input);
		bw4.close();
		File file5=new File("Name.txt");
		FileWriter f5=new FileWriter(file5);
		BufferedWriter bw5=new BufferedWriter(f5);
		input="";
		message=reader.readLine();
		while((message)!=null&&(!(message.equals("//")))){
			input=input+message+"\n";
			message=reader.readLine();
		}
		bw5.write(input);
		bw5.close();
		sock.close();
	}
	public void sendData() throws Exception{
		BufferedReader br;
		File file1=new File("Book.txt");
 		br=new BufferedReader(new FileReader(file1));
        String message;
        int i=0;
		while((message=br.readLine())!=null){
			writer.println(message);
			writer.flush();
		}
		writer.println("//");
		writer.flush();
		File file2=new File("Sales.txt");
		br=new BufferedReader(new FileReader(file2));
		while((message=br.readLine())!=null){
			writer.println(message);
			writer.flush();
		}
		writer.println("//");
		writer.flush();
		File file3=new File("DengLu.txt");
		br=new BufferedReader(new FileReader(file3));
		while((message=br.readLine())!=null){
			writer.println(message);
			writer.flush();
		}
		writer.println("//");
		writer.flush();
		File file4=new File("LogOn.txt");
		br=new BufferedReader(new FileReader(file4));
		while((message=br.readLine())!=null){
			writer.println(message);
			writer.flush();
		}
		writer.println("//");
		writer.flush();
		File file5=new File("Name.txt");
		br=new BufferedReader(new FileReader(file5));
		while((message=br.readLine())!=null){
			writer.println(message);
			writer.flush();
		}
		writer.println("//");
		writer.flush();
		file1.deleteOnExit();
		file2.deleteOnExit();
		file3.deleteOnExit();
		file4.deleteOnExit();
		file5.deleteOnExit();
	}
}
